package com.ynding;

import java.applet.Applet;
import java.awt.*;

/**
 * <p> 图像放大器</p>
 *
 * @author ynding
 * @version 2021/4/14
 **/
public class Magnify extends Applet {
    Image img[];
    int num = 0;
    int imgcount = 8;
    Choice choice1;
    Label label1;
    int mx = 70, my = 150;
    int imgheight, imgwidth;
    Checkbox checkbox1, checkbox2;
    boolean boolbox1 = false, boolbox2 = false, booldown = false;
    int clipx = 300, clipy = 300;

    @Override
    public void init() {
        resize(480, 400);
        img = new Image[imgcount];
        label1 = new Label("选择图像：");
        add(label1);
        choice1 = new Choice();
        choice1.addItem("晨曦的小屋");
        choice1.addItem("云中峭壁");
        choice1.addItem("雪山");
        choice1.addItem("长春蔓");
        add(choice1);
        checkbox1 = new Checkbox("反色");
        checkbox2 = new Checkbox("裁减区域");
        add(checkbox1);
        add(checkbox2);
        MediaTracker tracker = new MediaTracker(this);
        for (int i = 0; i < imgcount; i++) {
            img[i] = getImage(getCodeBase(), "map" + String.valueOf(i + 1) + ".jpg");
            tracker.addImage(img[i], i);
            try {
                tracker.waitForID(i);
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        int tmp = num;
        int inx, iny;
        if (boolbox1) {
            tmp = tmp + 4;
        }
        imgwidth = img[tmp].getWidth(this);
        imgheight = img[tmp].getHeight(this);
        g.drawRect(19, 100, 101, 101);
        g.drawImage(img[tmp], 20, 101, 100, 100, this);
        Graphics gr = g.create();
        gr.drawRect(124, 39, clipx + 1, clipy + 1);
        gr.clipRect(125, 40, clipx, clipy);
        inx = clipx / 2 + 125;
        iny = clipy / 2 + 40;
        gr.drawImage(img[tmp], inx - (int) (imgwidth / 100 * (mx - 20)), iny - (int) (imgheight / 100 * (my - 101)), this);

    }

    @Override
    public boolean action(Event e, Object o) {
        if (e.target.equals(choice1)) {
            num = choice1.getSelectedIndex();
        }
        if (e.target.equals(checkbox1)) {
            boolbox1 = !boolbox1;
        }
        if (e.target.equals(checkbox2)) {
            boolbox2 = !boolbox2;
        }
        repaint();
        return true;
    }

    @Override
    public boolean mouseMove(Event e, int x, int y) {

        if ((x > 20) && (x < 120) && (y > 101) && (y < 201)) {
            if (((mx - x) > 5) || ((x - mx) > 5) || ((my - y) > 5) || ((y - my) > 5)) {
                mx = x;
                my = y;
                if (mx < (35 + (clipx - 300) / 10)) {
                    mx = 35 + (clipx - 300) / 10;
                } else if (mx > (106 - (clipx - 300) / 10)) {
                    mx = 106 - (clipx - 300) / 10;
                }
                if (my < (123 + (clipy - 300) / 7)) {
                    my = 123 + (clipy - 300) / 7;
                } else if (my > (188 - (clipy - 300) / 7)) {
                    my = 188 - (clipy - 300) / 7;
                }
                Graphics g = getGraphics();
                paint(g);
            }
        }
        return true;
    }

    @Override
    public boolean mouseDown(Event e, int x, int y) {
        if (boolbox2) {
            if ((x > clipx + 123) && (x < clipx + 127) && (y > clipy + 38) && (y < clipy + 42)) {
                booldown = true;
            }
        }
        return true;
    }

    @Override
    public boolean mouseUp(Event e, int x, int y) {
        if (booldown) {
            booldown = false;
        }
        return true;
    }

    @Override
    public boolean mouseDrag(Event e, int x, int y) {
        if (booldown) {
            if ((clipx > x - 130) || (clipx > x - 120) || (clipy > y - 235) || (clipy > y - 245)) {
                clipx = x - 125;
                clipy = y - 40;
                if (x < 225) {
                    clipx = 225 - 125;
                } else if (x > 625) {
                    clipx = 625 - 125;
                }
                if (y < 140) {
                    clipy = 140 - 40;
                } else if (y > 540) {
                    clipy = 540 - 40;
                }
                repaint();
            }
        }
        return true;
    }
}
